package com.bistu.xiong.tulingrobotchat.activities;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.FrameLayout;

import com.bistu.xiong.common.app.Activity;
import com.bistu.xiong.common.helper.NavHelper;
import com.bistu.xiong.factory.persistence.Account;
import com.bistu.xiong.tulingrobotchat.R;
import com.bistu.xiong.tulingrobotchat.frags.main.CommunityFragment;
import com.bistu.xiong.tulingrobotchat.frags.main.HomeFragment;
import com.bistu.xiong.tulingrobotchat.frags.main.MeFragment;

import net.qiujuer.genius.ui.Ui;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends Activity implements BottomNavigationView.OnNavigationItemSelectedListener,NavHelper.OnTabChangedListener<Integer>{

    @BindView(R.id.bottomNavigationView)
    BottomNavigationView mNavigation;
    @BindView(R.id.lay_container)
    FrameLayout layContainer;
    @BindView(R.id.btn_action)
    FloatingActionButton mAction;

    private NavHelper<Integer> mNavHelper;

    /**
     * MainActivity 显示的入口
     * @param context 上下文
     */
    public static void show(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        // 判断账户信息是否完整
        if(Account.isComplete()){
            return super.initArgs(bundle);
        }else{
            //如果信息不完整走更新用户信息
            UpdateUserActivity.show(this);
            return false;
        }

    }

    @Override
    protected int getCountentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // 转接事件流到工具中
        return mNavHelper.performClickMenu(item.getItemId());
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        //初始化底部工具类
        mNavHelper = new NavHelper<>(this,R.id.lay_container,getSupportFragmentManager(),this);
        mNavHelper.add(R.id.tab_homepage,new NavHelper.Tab<>(HomeFragment.class,R.string.tab_homepage))
                .add(R.id.tab_community,new NavHelper.Tab<>(CommunityFragment.class,R.string.tab_community))
                .add(R.id.tab_me,new NavHelper.Tab<>(MeFragment.class,R.string.tab_me));
        mNavigation.setOnNavigationItemSelectedListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        //从底部导航中接管我们的menu，然后进行手动的触发第一次点击
        Menu menu = mNavigation.getMenu();
        //触发首次点击Home
        menu.performIdentifierAction(R.id.tab_homepage,0);
    }

    @OnClick(R.id.btn_action)
    void onActionClick(){
        //点击浮动按钮后直接跳转发帖页面
        SendUserPostActivity.show(this);
    }
    /**
     * NavHelper 处理后回调的方法
     * @param newTab 新的Tab
     * @param oldTab 旧的Tab
     */
    @Override
    public void onTabChanged(NavHelper.Tab<Integer> newTab, NavHelper.Tab<Integer> oldTab) {
        //对浮动按钮进行隐藏与显示的动画
        float transY = 0;
        float rotation = 0;
        if (Objects.equals(newTab.extra,R.string.tab_community)){
            mAction.setImageResource(R.drawable.ic_add);
            rotation = -360;
        }else{
            transY = Ui.dipToPx(getResources(),76);
        }

        //开始动画
        //旋转，Y轴位移，弹性差值器，时间
        mAction.animate()
                .rotation(rotation)
                .translationY(transY)
                .setInterpolator(new AnticipateOvershootInterpolator(1))
                .setDuration(480)
                .start();

    }
}
